package io.virtualapp.hook_sdk.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
import io.virtualapp.hook_sdk.firewall.FireWallType;
import io.virtualapp.hook_sdk.firewall.UrlBean;
import io.virtualapp.hook_sdk.models.InstallAppBean;
import io.virtualapp.hook_sdk.models.NotesBean;

public class RiNotesHelper extends SQLiteOpenHelper {

    public static String name = "ri_notes.db";

    private static String app_table = "create table app_table (" +
            "id integer primary key autoincrement," +
            "app_name text not null," +
            "apk_path text not null," +
            "app_time bigint not null)";

    private static String notes_table = "create table notes_table (" +
            "id integer primary key autoincrement," +
            "app_id integer not null," +
            "app_interface text not null," +
            "interface_type integer not null," +
            "app_adopt boolean not null)";

    private static String url_table = "create table url_table (" +
            "id integer primary key autoincrement," +
            "hook_url text not null)";

    private static String type_table = "create table type_table (" +
            "id integer primary key autoincrement," +
            "type integer not null)";

    private static String white_table = "create table white_table (" +
            "id integer primary key autoincrement," +
            "whiteStr text not null)";

    public RiNotesHelper(Context context) {
        super(context, name, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(app_table);
        db.execSQL(notes_table);
        db.execSQL(url_table);
        db.execSQL(type_table);
        db.execSQL(white_table);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public boolean insert(ContentValues values,String tableName){
        SQLiteDatabase db = getWritableDatabase();
        long l = db.insert(tableName,null,values);
        if (l > 0)
            return true;
        return false;
    }

    public boolean insertes(List<ContentValues> array, String tableName){
        SQLiteDatabase db = getWritableDatabase();
        db.beginTransaction();
        boolean flag = true;
        try {
            for (ContentValues values : array){
                db.insert(tableName,null,values);
                db.setTransactionSuccessful();
            }
        }catch (Exception e){
            flag = false;
        }finally {
            db.endTransaction();
            db.close();
        }
        return flag;
    }

    public List<InstallAppBean> selApp(){
        List<InstallAppBean> list = new ArrayList<>();
        SQLiteDatabase db = getWritableDatabase();
        Cursor cursor = db.query("app_table",null,null,
                null,null,null,null);
        while (cursor.moveToNext()){
            InstallAppBean installAppBean = new InstallAppBean();
            installAppBean.id = cursor.getInt(cursor.getColumnIndex("id"));
            installAppBean.app_name = cursor.getString(cursor.getColumnIndex("app_name"));
            installAppBean.app_time = cursor.getLong(cursor.getColumnIndex("app_time"));
            installAppBean.apk_path = cursor.getString(cursor.getColumnIndex("apk_path"));
            list.add(installAppBean);
        }
        cursor.close();
        db.close();
        return list;
    }

    public List<NotesBean> selNotes(){
        List<NotesBean> list = new ArrayList<>();
        SQLiteDatabase db = getWritableDatabase();
        Cursor cursor = db.query("notes_table",null,null,
                null,null,null,null);
        while (cursor.moveToNext()){
            NotesBean installAppBean = new NotesBean();
            installAppBean.app_id = cursor.getInt(cursor.getColumnIndex("app_id"));
            installAppBean.app_interface = cursor.getString(cursor.getColumnIndex("app_interface"));
            installAppBean.interface_type = cursor.getInt(cursor.getColumnIndex("interface_type"));
            installAppBean.app_adopt = cursor.getInt(cursor.getColumnIndex("app_adopt"))>0;
            list.add(installAppBean);
        }
        cursor.close();
        db.close();
        return list;
    }

    public boolean delApps(String appName){
        int id = selApp(appName);
        SQLiteDatabase db = getWritableDatabase();
        db.delete("app_table","id=?",new String[]{""+id});
        db.delete("notes_table","app_id=?",new String[]{""+id});
        db.close();
        return true;
    }

    public int selApp(String name){
        int id = -1;
        SQLiteDatabase db = getWritableDatabase();
        Cursor cursor = db.query("app_table",new String[]{"id","app_name"},"app_name=?",
                new String[]{name},null,null,null);
        while (cursor.moveToNext()){
            id = cursor.getInt(cursor.getColumnIndex("id"));
        }
        cursor.close();
        db.close();
        return id;
    }

    public List<UrlBean>selUrl(){
        List<UrlBean> datas = new ArrayList<>();
        SQLiteDatabase db = getWritableDatabase();
        Cursor cursor = db.query("url_table",null,null,null,null,null,null);
        while (cursor.moveToNext()){
            UrlBean bean = new UrlBean();
            bean.id = cursor.getInt(cursor.getColumnIndex("id"));
            bean.hook_url = cursor.getString(cursor.getColumnIndex("hook_url"));
            datas.add(bean);
        }
        cursor.close();
        db.close();
        return datas;
    }

    public FireWallType selType(){
        FireWallType type = FireWallType.NONE;
        SQLiteDatabase db = getWritableDatabase();
        Cursor cursor = db.query("type_table",null,null,null,null,null,null);
        while (cursor.moveToNext()){
            int index = cursor.getInt(cursor.getColumnIndex("type"));
            if (index == 0)
                type = FireWallType.NONE;
            else if (index == 1)
                type = FireWallType.WHITE;
            else
                type = FireWallType.BLACK;
        }
        cursor.close();
        db.close();
        return type;
    }
    public void delUrl(int id){
        SQLiteDatabase db = getWritableDatabase();
        db.delete("url_table","id=?",new String[]{""+id});
        db.close();
    }
    public void delType(){
        SQLiteDatabase db = getWritableDatabase();
        db.delete("type_table",null,null);
        db.close();
    }

    public void delWhite(){
        SQLiteDatabase db = getWritableDatabase();
        db.delete("white_table",null,null);
        db.close();
    }

    public String selWhite(){
        SQLiteDatabase db = getWritableDatabase();
        Cursor cursor = db.query("white_table",null,null,
                null,null,null,null);
        String result = null;
        while (cursor.moveToNext()){
            result = cursor.getString(cursor.getColumnIndex("whiteStr"));
        }
        cursor.close();
        db.close();
        return result;
    }

}
